﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinKeyBoard.Helpers
{
    /// <summary>
    /// 窗体事件类
    /// </summary>
    public class FormHelper
    {
        #region 窗体移动DLL单元
        /// <summary>
        /// 要向窗口发送消息
        /// </summary>
        private const int WM_SYSCOMMAND = 0x0112;
        /// <summary>
        /// 要向窗口发送移动的消息
        /// </summary>
        private const int SC_MOVE = 0xF010;
        /// <summary>
        /// 标题栏
        /// </summary>
        private const int HTCAPTION = 0x0002;
        /// <summary>
        /// 为当前的应用程序释放鼠标的捕获
        /// </summary>
        /// <returns></returns>
        [DllImport("user32.dll")]
        private static extern bool ReleaseCapture();
        /// <summary>
        /// 调用一个窗口的窗口函数，将一条消息命令发给那个窗口。（需要注意的是，这里所说的窗口不是.Net中的窗体(Form)，而是相当于.Net中的控件。当然，System.Windows.Forms.Form也是一个Control.）
        /// </summary>
        /// <param name="hwnd">要接收消息的那个窗口的句柄</param>
        /// <param name="wMsg">消息的标识符</param>
        /// <param name="wParam">具体取决于发送的消息</param>
        /// <param name="IParam">具体取决于发送的消息</param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        private static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int IParam);


        [DllImport("user32.dll")]
        private static extern bool ShowWindow(IntPtr hwnd, bool bEnable);
        /// <summary>
        /// 显示窗体
        /// </summary>
        /// <param name="hwnd"></param>
        /// <param name="nCmdShow">0：SW_HIDE 隐藏窗口，并激活其他窗口；1：SW_SHOWNORMAL/SW_NORMAL 激活并正常大小显示窗口；2：SW_SHOWMINIMIZED 最小化窗口；3：SW_SHOWMAXIMIZED/SW_MAXIMIZE 最大化窗口；4：SW_SHOWNOACTIVATE 显示但不激活</param>
        /// <returns></returns>
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);

        #endregion
        /// <summary>
        /// 移动窗口
        /// </summary>
        /// <param name="handle">当前的窗口句柄</param>
        public static void MoveWindow(IntPtr handle)
        {
            //拖动窗体
            ReleaseCapture();
            SendMessage(handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }
        /// <summary>
        /// 自动靠边隐藏窗体
        /// </summary>
        /// <param name="form">主窗体</param>
        /// <param name="thickness">边缘的厚度，窗体停靠在边缘隐藏后留出来的可见部分的厚度</param>
        /// <param name="panelLeft">设置 panelLeft 的背景颜色</param>
        /// <param name="panelRight">设置 panelRight 的背景颜色</param>
        /// <returns></returns>
        public static bool AutoSideHideOrShow(Form form, int thickness, Panel panelLeft = null, Panel panelRight = null)
        {
            var sideThickness = thickness;//边缘的厚度，窗体停靠在边缘隐藏后留出来的可见部分的厚度

            //如果窗体最小化或最大化了则什么也不做
            if (form.WindowState == FormWindowState.Minimized || form.WindowState == FormWindowState.Maximized)
            {
                return false;
            }

            //如果鼠标在窗体内
            if (Cursor.Position.X >= form.Left && Cursor.Position.X < form.Right && Cursor.Position.Y >= form.Top && Cursor.Position.Y < form.Bottom)
            {
                //如果窗体离屏幕边缘很近，则自动停靠在该边缘
                if (form.Top <= sideThickness)
                {
                    form.Top = 0;
                }
                if (form.Left <= sideThickness)
                {
                    form.Left = 0;
                }
                if (form.Left >= Screen.PrimaryScreen.WorkingArea.Width - form.Width - sideThickness)
                {
                    form.Left = Screen.PrimaryScreen.WorkingArea.Width - form.Width;
                }
                return false;
                //panelLeft.BackColor = Color.White;//初始颜色SystemColors.Control
                //panelRight.BackColor = Color.White;
            }
            //当鼠标离开窗体以后
            else
            {
                //隐藏到屏幕左边缘
                if (form.Left <= 0)
                {
                    //panelRight.BackColor = Color.Red;
                    form.Left = sideThickness - form.Width;
                }
                //隐藏到屏幕右边缘
                else if (form.Left >= Screen.PrimaryScreen.WorkingArea.Width - form.Width)
                {
                    //panelLeft.BackColor = Color.Red;
                    form.Left = Screen.PrimaryScreen.WorkingArea.Width - sideThickness;
                }
                //隐藏到屏幕上边缘
                else if (form.Top == 0 && form.Left > 0 && form.Left < Screen.PrimaryScreen.WorkingArea.Width - form.Width)
                {
                    //panelLeft.BackColor = Color.Red;
                    //panelRight.BackColor = Color.Red;
                    form.Top = sideThickness - form.Height;
                }
                return true;
            }
        }

    }
}
